home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / prospero / propsero.lha / prospero-beta.4.2e / app / cat.c next >
C/C++ Source or Header  |  1992-02-10  |  6KB  |  256 lines

  1. /*
  2.  * Derived from Berkeley source code.  Those parts are
  3.  * Copyright (c) 1989 The Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * This code is derived from software contributed to Berkeley by
  7.  * Kevin Fall.
  8.  *
  9.  * Redistribution and use in source and binary forms are permitted
  10.  * provided that: (1) source distributions retain this entire copyright
  11.  * notice and comment, and (2) distributions including binaries display
  12.  * the following acknowledgement:  ``This product includes software
  13.  * developed by the University of California, Berkeley and its contributors''
  14.  * in the documentation or other materials provided with the distribution
  15.  * and in all advertising materials mentioning features or use of this
  16.  * software. Neither the name of the University nor the names of its
  17.  * contributors may be used to endorse or promote products derived
  18.  * from this software without specific prior written permission.
  19.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  20.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  21.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  22.  */
  23.  
  24. #ifndef lint
  25. char copyright[] =
  26. "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
  27.  All rights reserved.\n";
  28. #endif /* not lint */
  29.  
  30. #ifndef lint
  31. static char sccsid[] = "@(#)cat.c    5.11 (Berkeley) 5/31/90";
  32. #endif /* not lint */
  33.  
  34. #include <sys/param.h>
  35. #include <sys/stat.h>
  36. #include <sys/file.h>
  37. #include <stdio.h>
  38. #include <ctype.h>
  39.  
  40. extern     char        *sys_errlist[];
  41. extern  int        sys_nerr;
  42. char            *strerror();
  43.  
  44. extern int errno;
  45. int bflag, eflag, nflag, sflag, tflag, vflag;
  46. int rval;
  47. char *filename;
  48.  
  49. main(argc, argv)
  50.     int argc;
  51.     char **argv;
  52. {
  53.     extern int optind;
  54.     int ch;
  55.     char *strerror();
  56.  
  57.     while ((ch = getopt(argc, argv, "benstuv")) != EOF)
  58.         switch (ch) {
  59.         case 'b':
  60.             bflag = nflag = 1;    /* -b implies -n */
  61.             break;
  62.         case 'e':
  63.             eflag = vflag = 1;    /* -e implies -v */
  64.             break;
  65.         case 'n':
  66.             nflag = 1;
  67.             break;
  68.         case 's':
  69.             sflag = 1;
  70.             break;
  71.         case 't':
  72.             tflag = vflag = 1;    /* -t implies -v */
  73.             break;
  74.         case 'u':
  75.             setbuf(stdout, (char *)NULL);
  76.             break;
  77.         case 'v':
  78.             vflag = 1;
  79.             break;
  80.         case '?':
  81.             (void)fprintf(stderr,
  82.                 "usage: cat [-benstuv] [-] [file ...]\n");
  83.             exit(1);
  84.         }
  85.     argv += optind;
  86.  
  87.     if (bflag || eflag || nflag || sflag || tflag || vflag)
  88.         cook_args(argv);
  89.     else
  90.         raw_args(argv);
  91.     exit(rval);
  92. }
  93.  
  94. cook_args(argv)
  95.     char **argv;
  96. {
  97.     register FILE *fp;
  98.  
  99.     fp = stdin;
  100.     filename = "-";
  101.     do {
  102.         if (*argv) {
  103.             if (!strcmp(*argv, "-"))
  104.                 fp = stdin;
  105.             else if (!(fp = fopen(*argv, "r"))) {
  106.                 (void)fprintf(stderr, 
  107.                     "cat: %s: %s\n", *argv, strerror(errno));
  108.                 rval = 1;
  109.                 ++argv;
  110.                 continue;
  111.             }
  112.             filename = *argv++;
  113.         }
  114.         cook_buf(fp);
  115.         if (fp != stdin)
  116.             (void)fclose(fp);
  117.     } while (*argv);
  118. }
  119.  
  120. cook_buf(fp)
  121.     register FILE *fp;
  122. {
  123.     register int ch, gobble, line, prev;
  124.  
  125.     line = gobble = 0;
  126.     for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
  127.         if (prev == '\n') {
  128.             if (ch == '\n') {
  129.                 if (sflag) {
  130.                     if (!gobble && putchar(ch) == EOF)
  131.                         break;
  132.                     gobble = 1;
  133.                     continue;
  134.                 }
  135.                 if (nflag && !bflag) {
  136.                     (void)fprintf(stdout, "%6d\t", ++line);
  137.                     if (ferror(stdout))
  138.                         break;
  139.                 }
  140.             } else if (nflag) {
  141.                 (void)fprintf(stdout, "%6d\t", ++line);
  142.                 if (ferror(stdout))
  143.                     break;
  144.             }
  145.         }
  146.         gobble = 0;
  147.         if (ch == '\n') {
  148.             if (eflag)
  149.                 if (putchar('$') == EOF)
  150.                     break;
  151.         } else if (ch == '\t') {
  152.             if (tflag) {
  153.                 if (putchar('^') == EOF || putchar('I') == EOF)
  154.                     break;
  155.                 continue;
  156.             }
  157.         } else if (vflag) {
  158.             if (!isascii(ch)) {
  159.                 if (putchar('M') == EOF || putchar('-') == EOF)
  160.                     break;
  161.                 ch = toascii(ch);
  162.             }
  163.             if (iscntrl(ch)) {
  164.                 if (putchar('^') == EOF ||
  165.                     putchar(ch == '\177' ? '?' :
  166.                     ch | 0100) == EOF)
  167.                     break;
  168.                 continue;
  169.             }
  170.         }
  171.         if (putchar(ch) == EOF)
  172.             break;
  173.     }
  174.     if (ferror(fp)) {
  175.         (void)fprintf(stderr, "cat: %s: read error\n", filename);
  176.         rval = 1;
  177.     }
  178.     if (ferror(stdout)) {
  179.         clearerr(stdout);
  180.         (void)fprintf(stderr, "cat: stdout: write error\n");
  181.         rval = 1;
  182.     }
  183. }
  184.  
  185. raw_args(argv)
  186.     char **argv;
  187. {
  188.     register int fd;
  189.  
  190.     fd = fileno(stdin);
  191.     filename = "-";
  192.     do {
  193.         if (*argv) {
  194.             if (!strcmp(*argv, "-"))
  195.                 fd = fileno(stdin);
  196.             else if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
  197.                 (void)fprintf(stderr, "cat: %s: %s\n",
  198.                     *argv, strerror(errno));
  199.                 rval = 1;
  200.                 ++argv;
  201.                 continue;
  202.             }
  203.             filename = *argv++;
  204.         }
  205.         rval |= raw_cat(fd);
  206.         if (fd != fileno(stdin))
  207.             (void)close(fd);
  208.     } while (*argv);
  209. }
  210.  
  211. raw_cat(fd)
  212.     register int fd;
  213. {
  214.     register int nr, nw, off;
  215.     static int bsize;
  216.     static char *buf;
  217.     struct stat sbuf;
  218.     char *malloc(), *strerror();
  219.  
  220.     if (!buf) {
  221.         if (fstat(fileno(stdout), &sbuf)) {
  222.             (void)fprintf(stderr, "cat: %s: %s\n", filename,
  223.                 strerror(errno));
  224.             return(1);
  225.         }
  226.         bsize = MAX(sbuf.st_blksize, 1024);
  227.         if (!(buf = malloc((u_int)bsize))) {
  228.             (void)fprintf(stderr, "cat: %s: no memory.\n",
  229.                 filename);
  230.             return(1);
  231.         }
  232.     }
  233.     while ((nr = read(fd, buf, bsize)) > 0)
  234.         for (off = 0; off < nr;) {
  235.             if ((nw = write(fileno(stdout), buf + off, nr)) < 0) {
  236.                 perror("cat: stdout");
  237.                 return(1);
  238.             }
  239.             off += nw;
  240.         }
  241.     if (nr < 0) {
  242.         (void)fprintf(stderr, "cat: %s: %s\n", filename,
  243.             strerror(errno));
  244.         return(1);
  245.     }
  246.     return(0);
  247. }
  248.  
  249. char *strerror(number)
  250.     int        number;
  251.     {
  252.     if((number > 0) && (number < sys_nerr))
  253.         return(sys_errlist[number]); 
  254.     else return("Unknown Error");
  255.     }
  256.